import { Injectable } from "@nestjs/common";
import { JwtService } from "@nestjs/jwt";
import { UsersEntity } from "src/Entity/user.entity";
import { UsersService } from "src/service/users.service";

@Injectable()
export class AuthService {
    constructor(
        private usersService: UsersService,
        private jwtService: JwtService
    ) { }

    async validateUser(username: string, pass: string): Promise<any> {
        const user = await this.usersService.findOne(username);
        if (user && user.password === pass) {
            const { password, ...result } = user;
            return result;
        }
        return null;
    }

    async login(user: any): Promise<any> {
        const userEntity = await this.usersService.findOne(user.username);
        const payload = { username: userEntity.username, sub: userEntity.id };
        if(userEntity)
        return {
            access_token: this.jwtService.sign(payload),
        };
    }

    // async addUser(usersEntity : UsersEntity){
    //     return this.usersService.create(usersEntity); 
    // }

    // async findAll(){
    //     return this.usersService.findAll();
    // }

    // async deleteById(userId : number){

    //     return this.usersService.remove(userId);
    // }
    
    // async updateUser(usersEntity : UsersEntity){
    //     return this.usersService.update(usersEntity);
    // }

    // async findOne(username : string){
    //     return this.usersService.findOne(username);
    // }


}